package com.ssy.lingxi.report.config;

import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateSettings;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.Map;

@Configuration
public class DataSourceConfig {
    @Resource
    private JpaProperties jpaProperties;
    @Resource
    private HibernateProperties hibernateProperties;

    @Bean(name = "reportDataSource")
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource.hikari.report")
    public HikariDataSource reportDataSource() {
        return new HikariDataSource();
    }

    @Bean(name = "orderDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.hikari.order")
    public HikariDataSource secondaryDataSource() {
        return new HikariDataSource();
    }

    @Bean(name = "merchantMemberDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.hikari.merchantmember")
    public HikariDataSource merchantMemberDataSource() {
        return new HikariDataSource();
    }

    @Bean(name = "platformTemplateDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.hikari.platformtemplate")
    public HikariDataSource platformTemplateDataSource() {
        return new HikariDataSource();
    }

    @Bean(name = "productDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.hikari.product")
    public HikariDataSource productDataSource() {
        return new HikariDataSource();
    }

    @Bean(name = "payDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.hikari.pay")
    public HikariDataSource payDataSource() {
        return new HikariDataSource();
    }

    @Bean(name = "settleAccountDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.hikari.settleaccount")
    public HikariDataSource settleAccountDataSource() {
        return new HikariDataSource();
    }

    @Bean(name = "afterSaleDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.hikari.aftersale")
    public HikariDataSource afterSaleDataSource() {
        return new HikariDataSource();
    }

    @Bean(name = "logisticsDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.hikari.logistics")
    public HikariDataSource logisticsDataSource() {
        return new HikariDataSource();
    }

    @Bean(name = "enhanceDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.hikari.enhance")
    public HikariDataSource enhanceDataSource() {
        return new HikariDataSource();
    }

    @Bean(name = "platformManageDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.hikari.platformmanage")
    public HikariDataSource platformManageDataSource() {
        return new HikariDataSource();
    }

    @Bean(name = "contractDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.hikari.contract")
    public HikariDataSource contractDataSource() {
        return new HikariDataSource();
    }

    @Bean(name = "purchaseDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.hikari.purchase")
    public HikariDataSource purchaseDataSource() {
        return new HikariDataSource();
    }

    /**
     * 配置 组合jpaProperties和hibernateProperties配置的map对象
     * @return 组合jpaProperties和hibernateProperties配置的map
     */
    @Bean(name = "vendorProperties")
    public Map<String, Object> getVendorProperties() {
        return hibernateProperties.determineHibernateProperties(jpaProperties.getProperties(), new HibernateSettings());
    }

    /**
     * 报表JdbcTemplate
     */
    @Bean(name = "reportJdbcTemplate")
    public JdbcTemplate reportJdbcTemplate(@Qualifier("reportDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    /**
     * 订单JdbcTemplate
     */
    @Bean(name = "orderJdbcTemplate")
    public JdbcTemplate secondaryJdbcTemplate(@Qualifier("orderDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    /**
     * 会员JdbcTemplate
     */
    @Bean(name = "merchantMemberJdbcTemplate")
    public JdbcTemplate merchantMemberJdbcTemplate(@Qualifier("merchantMemberDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    /**
     * 模板JdbcTemplate
     */
    @Bean(name = "platformTemplateJdbcTemplate")
    public JdbcTemplate platformTemplateJdbcTemplate(@Qualifier("platformTemplateDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    /**
     * 商品JdbcTemplate
     */
    @Bean(name = "productJdbcTemplate")
    public JdbcTemplate productJdbcTemplate(@Qualifier("productDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    /**
     * 支付JdbcTemplate
     */
    @Bean(name = "payJdbcTemplate")
    public JdbcTemplate payJdbcTemplate(@Qualifier("payDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    /**
     * 结算JdbcTemplate
     */
    @Bean(name = "settleAccountJdbcTemplate")
    public JdbcTemplate settleAccountJdbcTemplate(@Qualifier("settleAccountDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    /**
     * 售后JdbcTemplate
     */
    @Bean(name = "afterSaleJdbcTemplate")
    public JdbcTemplate afterSaleJdbcTemplate(@Qualifier("afterSaleDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    /**
     * 物流JdbcTemplate
     */
    @Bean(name = "logisticsJdbcTemplate")
    public JdbcTemplate logisticsJdbcTemplate(@Qualifier("logisticsDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    /**
     * 加工JdbcTemplate
     */
    @Bean(name = "enhanceJdbcTemplate")
    public JdbcTemplate enhanceJdbcTemplate(@Qualifier("enhanceDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    /**
     * 平台管理JdbcTemplate
     */
    @Bean(name = "platformManageJdbcTemplate")
    public JdbcTemplate platformManageJdbcTemplate(@Qualifier("platformManageDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    /**
     * 合同JdbcTemplate
     */
    @Bean(name = "contractJdbcTemplate")
    public JdbcTemplate contractJdbcTemplate(@Qualifier("contractDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    /**
     * 采购JdbcTemplate
     */
    @Bean(name = "purchaseJdbcTemplate")
    public JdbcTemplate purchaseJdbcTemplate(@Qualifier("purchaseDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }
}
